<?php

namespace App\Http\Controllers\Api;

use App\Attr;
use App\Http\Requests\StoreAttr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class AttrController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : $this->orderBy;
        $orderType = $request->order_type ? $request->order_type : $this->orderType;
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = Attr::orderBy($orderBy, $orderType);
        if ($request->name) {
            $lists->where('name', 'like', '%'.$request->name.'%');
        }
        if ($request->type_id) {
            $lists->where('type_id', $request->type_id);
        }
        return $lists->with('type')->paginate($pageSize);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAttr $request)
    {
        $data = $request->all();
        if ($request->input_type != 'SELECT')
            $data['values'] = null;
        Attr::create($data);
        return response(['message' => '创建成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Attr  $attr
     * @return \Illuminate\Http\Response
     */
    public function show(Attr $attr)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Attr  $attr
     * @return \Illuminate\Http\Response
     */
    public function edit(Attr $attr)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Attr  $attr
     * @return \Illuminate\Http\Response
     */
    public function update(StoreAttr $request, Attr $attr)
    {
        $data = $request->all();
        if ($request->input_type != 'SELECT')
            $data['values'] = null;
        $attr->update($data);
        return response(['message' => '修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Attr  $attr
     * @return \Illuminate\Http\Response
     */
    public function destroy(Attr $attr)
    {
        $attr->delete();
        return response(['message' => '删除成功']);
    }

    public function byTypeAndGoods(Request $request)
    {
        if (!$request->type_id) {
            return response(['message' => '非法操作']);
        }
        $attrs = Attr::where('type_id', $request->type_id)->get();
        $goodsAttrs = null;
        if ($request->goods_id) {
            $goodsAttrs = DB::table('attr_goods')->where('goods_id', $request->goods_id)->get();
        }
        foreach($attrs as & $attr) {
            $attr->value='';
            if ($goodsAttrs) {
                foreach ($goodsAttrs as $goodsAttr) {
                    if ($attr->id == $goodsAttr->attr_id) {
                        $attr->value = $goodsAttr->value;
                    }
                }
            }
        }
        return $attrs;
    }
}
